package com.xhwl.centiotdata.service.integrateconfig;

import com.xhwl.centiotdata.pojo.dto.visitor.VisitorIntegrationConfigDTO;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.pojo.cent.vehicle.converter.config.ToVehicleIntegrationConfigBO;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

/**
 * @Author: yanyixiong
 * @Description: 访客配置
 * @Date: create in 9:41 2022/3/10
 */
@Component
public class VisitorIntegrationConfig extends IntegrationConfig<Integer, List<VisitorIntegrationConfigDTO>>{

    private Map<Integer, List<VisitorIntegrationConfigDTO>> visitorIntegrationConfigMap = new ConcurrentHashMap<>();

    @Override
    public List<VisitorIntegrationConfigDTO> getIntegrationConfig(Integer projectId) {
        return visitorIntegrationConfigMap.get(projectId);
    }

    public Map<Integer, List<VisitorIntegrationConfigDTO>> getIntegrationConfig() {
        return visitorIntegrationConfigMap;
    }

    @EventListener
    public void listenIntegrationConfig(IntegrationConfigEvent event) {
        this.handleVisitorIntegrationConfig();
    }

    private void handleVisitorIntegrationConfig() {
        visitorIntegrationConfigMap = IntegrationConfig.integrationConfigList.stream()
                .filter(deviceProjSupplierCategory -> CategoryEnum.VISITOR_SYSTEM.getId().equals(deviceProjSupplierCategory.getProdCategoryId()))
                .map(VisitorIntegrationConfigDTO::toVisitorIntegrationConfig)
                .collect(Collectors.groupingByConcurrent(VisitorIntegrationConfigDTO::getProjectId));

    }

}
